package lanQiaoBei.数据结构.并查集;

import java.io.*;

/*
 * 给定有n个点（1-n）的无向图，初始是图没有边
 * 现在要进行m个操作，操作有三种：
 * C1 a b 在a,b之间连一条边，a b可能相等
 * Q1 a b 询问a,b是否在同一个连通块中
 * Q2 a   询问a所在的连通块的数量
 * */
public class P2 {
    final static int N = 100010;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int n, m;
    static int[] p = new int[N], size = new int[N];

    static void scan() throws Exception {
        String[] ss = br.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        //init
        for (int i = 1; i <= n; i++) {
            p[i] = i;
            size[i] = 1;
        }
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    public static void main(String[] args) throws Exception {
        scan();
        while (m-- > 0) {
            String[] ss = br.readLine().split(" ");
            int a = Integer.parseInt(ss[1]), b = ss.length == 3 ? Integer.parseInt(ss[2]) : 0;
            if ("C".equals(ss[0])) {
                int fa = find(a), fb = find(b);
                if (fa == fb) continue;
                p[fa] = fb;
                size[fb] += size[fa];
            } else if ("Q1".equals(ss[0])) {
                if (find(a) == find(b)) System.out.println("yes");
                else System.out.println("not");
            } else {
                System.out.println(size[find(a)]);
            }
        }
    }
}
